package edu.utn.tacs.grupouno.ui.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.VerticalPanel;

import edu.utn.tacs.grupouno.ui.client.dto.EstudioDTO;
import edu.utn.tacs.grupouno.ui.client.dto.IndicadorDTO;

public class EstudioPanel extends VerticalPanel {
	
	private String id;

	public EstudioPanel(String id) {
		this.id = id;
		initComponents();
	}

	private void initComponents() {
		add(new Label("Cargando..."));
		final Panel me = this;
		EstudiosUIServiceAsync service = GWT.create(EstudiosUIService.class);
		service.getEstudio(id, new AsyncCallback<EstudioDTO>() {
			@Override
			public void onSuccess(EstudioDTO estudio) {
				me.clear();
				Label title = new Label(estudio.getNombre());
				title.setStyleName("title");
				VerticalPanel container = new VerticalPanel();
				Label label = new Label("Indicadores:");
				label.setStyleName("subTitle");
				container.add(label);
				
				IndicadorPanelFactory factory = new IndicadorPanelFactory();
				for(IndicadorDTO indicador : estudio.getIndicadores()) {
					DecoratorPanel panel = new DecoratorPanel();
					panel.setStyleName("detalleCell");
					panel.add(factory.create(indicador));
					container.add(panel);
				}
				me.add(title);
				me.add(container);
			}
			
			@Override
			public void onFailure(Throwable t) {
				me.clear();
				me.add(new Label(t.getMessage()));
			}
		});
	}

}
